const mysql = require('mysql2')

const {
  MYSQL_HOST,
	MYSQL_PORT,
	MYSQL_DATABASE,
	MYSQL_USER,
	MYSQL_PASSWORD
} = require('./config')

const pool = mysql.createPool({
  host: MYSQL_HOST,
  port: MYSQL_PORT,
  database: MYSQL_DATABASE,
  user: MYSQL_USER,
  password: MYSQL_PASSWORD,
  connectionLimit: 10
})

// console.log('pool---', pool)

pool.getConnection(function(err, conn) {
  conn.connect((err) => {
    if (err) {
      console.log('数据库连接失败：', err);
    } else {
      console.log('数据库连接成功~');
    }
  })
})

// 测试代码
// const statement = `SHOW DATABASES;`
// pool.query(statement, (err, results, fields) => {
//   console.log('results---', results);
// })

module.exports = pool.promise()